package net.microfigure.controller;

import javax.validation.Valid;

import java.util.List;
import java.util.Date;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import net.microfigure.commons.result.PageInfo;
import net.microfigure.commons.shiro.ShiroUser;
import net.microfigure.commons.utils.IDUtils;
import net.microfigure.model.FamilyInfo;
import net.microfigure.model.PartInst;
import net.microfigure.service.IFamilyInfoService;
import net.microfigure.service.IPartInstService;
import net.microfigure.commons.base.BaseController;

/**
 * <p>
 * 参合登记实例表 前端控制器
 * </p>
 *
 * @author 林景锋
 * @since 2018-06-22
 */
@Controller
@RequestMapping("/partInst")
public class PartInstController extends BaseController {

	@Autowired 
	private IPartInstService partInstService;
	@Autowired 
	private IFamilyInfoService familyInfoService;



	@GetMapping("/manager")
	public String manager() {
		System.out.println("跳转admin/partInst/familyPartList");
		return "admin/partInst/familyPartList";
	}


	@GetMapping("/managerAll")
	public String managerAll() {
		return "admin/partInst/partInstAll";
	}

	/*	@PostMapping("/dataGrid")
	@ResponseBody
	public Object dataGrid(Integer page,Integer rows,String sort, String order) {
		PageInfo pageInfo = new PageInfo(page, rows, sort, order);


		return pageInfo;

	}*/


	@PostMapping("/dataGrid")
	@ResponseBody
	public Object dataGrid(Integer page,Integer rows,String sort, String order,String familyNo) {
		//public Object dataGrid(Integer page,Integer rows,String sort, String order,String housemaster) {
		System.out.println("找到了这个");
		PageInfo pageInfo = new PageInfo(page, rows, sort, order);
		//		ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal(); 
		//		String name = user.getName();
		//		String housemaster = "李明";
		//		System.out.println("输入的用户名"+housemaster);
		//familyInfoService.selectDataGrid(pageInfo,familyNo);
		partInstService.selectDataGrid(pageInfo,familyNo);
		System.out.println("找到了这个PartInfoController");
		return pageInfo;
	}
	@PostMapping("/dataGridAll")
	@ResponseBody
	public Object dataGridAll(Integer page,Integer rows,String sort, String order) {
		PageInfo pageInfo = new PageInfo(page, rows, sort, order);

		partInstService.selectDataGrid(pageInfo);
		return pageInfo;
	}


	/**
	 * 添加页面
	 * @return
	 */
	@GetMapping("/addPage")
	public String addPage() {
		return "admin/partInst/partInstAdd";
	}

	/**
	 * 添加
	 * @param 
	 * @return
	 */
	@PostMapping("/add")
	@ResponseBody
	public Object add(@Valid PartInst partInst) {

		boolean b = partInstService.insert(partInst);
		if (b) {
			return renderSuccess("添加成功！");
		} else {
			return renderError("添加失败！");
		}
	}



	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@PostMapping("/delete")
	@ResponseBody
	public Object delete(String partNo) {

		System.out.println("public Object delete(String partNo)"+partNo);
		partInstService.cancelPart(partNo);

		return renderSuccess("删除成功！");

	}

	/**
	 * 编辑
	 * @param model
	 * @param id
	 * @return
	 */
	@GetMapping("/editPage")
	public String editPage(Model model, Long id) {
		PartInst partInst = partInstService.selectById(id);
		model.addAttribute("partInst", partInst);
		return "admin/partInst/partInstEdit";
	}

	/**
	 * 添加参合登记实例表信息
	 * 
	 * @param PartInst
	 * @param model
	 */

	@PostMapping("/createOK")
	@ResponseBody
	public Object createOK(@Valid PartInst partInst, Model model) {

		System.out.println("强行参合打印"+partInst.getPartNo());
		partInstService.addPartInst(partInst);

		return renderSuccess("编辑成功！");
	}
	/**
	 * 强行让你参合
	 * @param model
	 * @param id
	 * @return
	 */
	@GetMapping("/addPageOK")
	public String addPageOK(Model model, Long id) {
		System.out.println("找到这个先。");
		FamilyInfo familyInfo = familyInfoService.selectById(id);
		System.out.println("强行让你参合页面跳转");
		PartInst partInst = new PartInst();
		BeanUtils.copyProperties(familyInfo, partInst);
		String partInstNo = partInstService.getPartInstNo(familyInfo.getFamilyNo());
		partInst.setPartNo(partInstNo);


		model.addAttribute("partInst", partInst);
		return "admin/partInst/partInstOK";
	}
	/**
	 * 编辑
	 * @param 
	 * @return
	 */
	@PostMapping("/edit")
	@ResponseBody
	public Object edit(@Valid PartInst partInst) {

		boolean b = partInstService.updateById(partInst);
		if (b) {
			return renderSuccess("编辑成功！");
		} else {
			return renderError("编辑失败！");
		}
	}




}
